import Vue from 'vue'
import App from './App.vue'
import router from './router'

import 'lib-flexible/flexible'
import axios from 'axios'
import VueAxios from 'vue-axios'
import store from './store'

import Video from 'video.js'
import 'video.js/dist/video-js.css'
import CryptoJS from 'crypto-js'
Vue.use(CryptoJS)

import VueRouter from 'vue-router'

import {
    Tabbar,
    TabbarItem,
    Icon,
    Tab,
    Tabs,
    Swipe,
    SwipeItem,
    DropdownMenu,
    DropdownItem,
    List,
    Sidebar,
    SidebarItem,
    Button,
    Tag,
    Search,
    Popover,
    Popup,
    IndexBar,
    IndexAnchor,
    Cell,
    TreeSelect,
    NavBar,
    Form,
    Field,
    CellGroup,
    ShareSheet,
    Rate,
    Badge,
    Loading,
    Toast,
    Empty,
    Checkbox,
    CheckboxGroup,
    Collapse,
    CollapseItem
}
from 'vant'

Vue
    .use(Tabbar).use(TabbarItem).use(Icon)
    .use(Tab).use(Tabs).use(Swipe).use(SwipeItem)
    .use(DropdownMenu).use(DropdownItem).use(List)
    .use(Sidebar).use(SidebarItem).use(Button).use(Tag)
    .use(Search).use(Popover).use(Popup).use(IndexBar)
    .use(IndexAnchor).use(Cell).use(TreeSelect).use(NavBar)
    .use(Form).use(Field).use(CellGroup).use(ShareSheet)
    .use(Rate).use(Badge).use(Loading).use(Empty)
    .use(Checkbox).use(CheckboxGroup).use(Collapse).use(CollapseItem)

Vue.use(VueAxios, axios)


// 设置请求基础路径
axios.defaults.baseURL = 'https://apis.netstart.cn/maoyan/';

// 添加一个axios请求之前拦截器
axios.interceptors.request.use(config => {
    // 处理请求的公共参数
    if (config.method === 'get') {
        config.params = config.params || {};
    } else if (config.method === 'post') {
        config.data = config.data || {};

        // 序列化post请求参数
        let dataParams = '';
        for (let key in config.data) {
            dataParams += `${key}=${config.data[key]}&`;
        }

        config.data = dataParams;

    }

    // 启动加载提示
    Toast.loading({
        message: '加载中...',
        // 禁止穿透点击
        forbidClick: true,
        loadingType: 'spinner',
        // 不会自动关闭提示
        duration: 0
    })

    return config;
}, err => {


    return Promise.reject(err);
})

// 添加一个响应之后拦截器
axios.interceptors.response.use(res => {
    // 

    // 关闭加载提示
    Toast.clear();

    return res;
}, err => {
    // 关闭加载提示
    Toast.clear();

    return Project.reject(err);
})

Vue.config.productionTip = false

// 处理图片宽高
Vue.filter('setWH', (url, arg) => {
    return url.replace(/w\.h/, arg);
})


// 路由前置守卫
router.beforeEach((to, from, next) => {
    if (to.meta.auth) {
        // if (to.name === 'My') {
        // 
        // if (to.meta.isLogin === true) {
        //     
        //     next();
        // } else {
        //     
        //     next({ name: 'Login' });
        // }
        const _token = localStorage.getItem('_token');
        // 
        if (_token) {
            // 若存在_token,则放行
            next();
        } else {
            // 若没有_token，则跳转登录页面
            next('/login');
        }
    } else {
        // 其它页面 路由放行
        // 
        next();
    }
})

// 点击相同路由报错的解放方法
// 捕获错误
let originPush = VueRouter.prototype.push;

// 重新VueRouter的原型方法
VueRouter.prototype.push = function(location) {
    // location 目标路由的信息
    // 

    originPush.call(this, location, () => {}, (err) => {
        // 捕获错误 err
        // 不输出即可
        // 
    })
}

new Vue({
    router,
    store,
    render: h => h(App)
}).$mount('#app')